from prg.common.import_file import *
from prg.common.common_base import CommonBase
from prg.proc.control_thread import RunControl
from prg.command_info import CommandInfo
from prg.proc.command_thread import RunCommand
from prg.proc.receive_svr_thread import RunReceiveSvr
from prg.common.pack_base import *
import socket
import queue
import time


class LinkCli:
    def __init__(self):
        self.is_link = False
        self.config = None
        self.public_item = PublicEntity.instance()
        cfg = ConfigBll.instance().get_base_config()
        self.udp_port = ConfigBll.instance().get_udp_port()
        self.command_process = RunCommand()
        self.svr_ip = cfg.host.ip
        self.svr_port = cfg.host.port
        self.svr_sign = cfg.authority.password
        self.svr_aes = cfg.authority.aes
        self.name = cfg.authority.name
        self.bind_list = queue.Queue()
        self.command_process.start()
        self.create_svr_link()
        self.link_to_svr()

    def exec_command(self, en):
        if "link" == en.command:
            self.link_to(en.arg)

    def create_svr_link(self):
        ip = self.svr_ip
        port = self.svr_port
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.connect((ip, port))
        svr_name = self.name
        arg = ArgBase()
        arg.sign = self.svr_sign
        reg_dict = CommonBase.aes_encryption_json(key=self.svr_aes, entity=arg)
        cmd_svr = CommandInfo(socket_item=server_socket, command="login", name=svr_name, arg=reg_dict)
        rec_trd = RunReceiveSvr(name=svr_name, socket_item=server_socket)
        rec_trd.run_listen(sign=self.svr_aes, udp_port=self.udp_port.get())
        self.public_item.server = (server_socket, rec_trd)
        self.command_process.add(cmd_svr)
        control = RunControl(control=self.exec_command)
        control.start()
        time.sleep(3)
        self.is_link = True
        print("server link started")

    def link_to(self, arg_item):
        socket_val, rec = self.public_item.server
        task_id, task_item = self.public_item.instance().get_task()
        cmd_link_en = CommonBase.dict_to_entity(dict_item=arg_item.arg_dict, en=CommandLinkEntity())
        task_item.link = cmd_link_en
        arg = ArgBase()
        arg.sign = self.svr_sign
        arg.arg_dict = arg_item.arg_dict
        arg.arg_dict["task"] = task_id
        arg.arg_dict["verification"] = CommonBase.aes_encryption(key=cmd_link_en.verification, data=rec.verify)
        arg_str = CommonBase.aes_encryption_json(key=self.svr_aes, entity=arg)
        cmd_svr = CommandInfo(socket_item=socket_val, command="to", name=self.name, arg=arg_str)
        self.command_process.add(cmd_svr)
        print("link start to: %s, verification: %s" % (cmd_link_en.to, cmd_link_en.verification))

    @test_pack
    def link_to_svr(self):
        time.sleep(5)
        arg = ArgBase()
        arg.arg_dict["to"] = "yujc"
        arg.arg_dict["local"] = 80
        arg.arg_dict["verification"] = ""
        arg.arg_dict["encryption"] = "sdfadsafdsafe"
        self.link_to(arg)
